package com.example.service;

import com.example.common.enums.ModuleEnum;
import com.example.controller.domain.SearchContent;
import com.example.entity.Collect;
import com.example.entity.Messages;
import com.example.entity.Notebook;
import com.example.entity.Works;
import com.example.mapper.CollectMapper;
import com.example.mapper.NotebookMapper;
import com.example.mapper.WorksMapper;
import com.example.utils.SearchRedisHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private MessagesService messagesService;
    @Resource
    private NotebookMapper notebookMapper;
    @Resource
    private WorksMapper worksMapper;
    @Resource
    private SearchRedisHelper searchRedisHelper;

    /**
     * 1. 收藏
     * 2. 取消收藏
     * 取决于当前的模块数据有没有被收藏
     */
    public void set(Collect collect) {
        List<Collect> collectList = collectMapper.selectCollect(collect);
        if (ObjectUtils.isEmpty(collectList)) {
            collect.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
            collect.setDelTag("0");
            //添加标志位
            collectMapper.insert(collect);
            Messages messages=new Messages();
            messages.setFid(collect.getFid());
            messages.setModule(collect.getModule());
            messages.setOptType(ModuleEnum.COLLECT.getValue());
            String dynamicName;
            if(messages.getModule().equals(ModuleEnum.NOTEBOOK.getValue())){
                Notebook notebook=notebookMapper.selectById(messages.getFid());
                dynamicName=notebook.getTitle();
                messages.setUserId(notebook.getUserId());
            }else{
                Works works=worksMapper.selectById(messages.getFid());
                dynamicName=works.getName();
                messages.setUserId(works.getUserId());
            }
            messagesService.add(messages,dynamicName);
            SearchContent searchContent=new SearchContent();
            searchContent.setName(dynamicName);
            searchContent.setFid(collect.getFid());
            searchContent.setModule(collect.getModule());
            searchRedisHelper.addRedisHotSearch(searchContent,2L);
        } else {
            collect.setDelTag("1");
            collect.setUpdateTime(Timestamp.valueOf(LocalDateTime.now()));
            List<Collect> isExist=collectMapper.selectCollect(collect);
            if (!ObjectUtils.isEmpty(isExist)){
                collect.setDelTag("0");
                collectMapper.update(collect);
            }else{
                collectMapper.update(collect);
            }
        }
    }

    public List<Collect> selectCollect(Integer fid, Integer userId, String module,String delTag) {
        Collect collect = new Collect();
        collect.setFid(fid);
        collect.setUserId(userId);
        collect.setModule(module);
        collect.setDelTag(delTag);
        return collectMapper.selectCollect(collect);
    }
    public void userDelete(Collect collect) {
        collect.setDelTag("1");
        collectMapper.update(collect);
    }

    public List<Collect> selectWorksAll() {
        return collectMapper.selectWorksAll();
    }
    public List<Collect> selectNotebookAll() {
        return collectMapper.selectNotebookAll();
    }
}
